//
//  SEGActivityInvitationViewController.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/8/19.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGActivityInvitationViewController.h"
#import "SEGActivityInvitationViewModel.h"
@interface SEGActivityInvitationViewController ()

@property (nonatomic, strong) SEGActivityInvitationViewModel *viewModel;
@end

@implementation SEGActivityInvitationViewController
- (instancetype)initWithActId:(NSString *)actId {
    if (self = [super init]) {
        self.viewModel = [[SEGActivityInvitationViewModel alloc] initWithActId:actId];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"邀请函";
    
    self.view.backgroundColor = kColor_bg;
    
    [self showCustomNavigationBackButton];
    [SVProgressHUD show];
    
    //请求活动详情
    [self.viewModel.activityManager requestQueryInvitationByActId:self.viewModel.actId finished:^(BOOL isSuccessed, id result, NSString* codeType, NSString* errorMessage) {
        [SVProgressHUD dismiss];
        if (isSuccessed) {
            [self seg_setUpSubviewsWithModel:self.viewModel.activityManager.invitationModel];
        }else {
            [SEGAlertView showTmepMessageWithMessage:errorMessage];
        }
    }];
}

- (void)seg_setUpSubviewsWithModel:(SEGActivityInvitaionModel *)model {
    
    
    UIImageView *bgImageView = [[UIImageView alloc] init];
    [self.view addSubview:bgImageView];
    [bgImageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kClientImageRootURL,model.bckPic]] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"pic_activity_bg")]];
    bgImageView.frame = self.view.bounds;
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];

    UIImageView *iconView = [[UIImageView alloc] init];
    [scrollView addSubview:iconView];
    iconView.layer.masksToBounds = YES;
    iconView.layer.cornerRadius = 29.5;
    iconView.contentMode = UIViewContentModeScaleAspectFit;
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(scrollView).offset(60);
        make.centerX.equalTo(scrollView);
        make.width.height.mas_equalTo(59);
    }];

    // 调整行间距
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:8];
    [paragraphStyle setAlignment:NSTextAlignmentCenter];

    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = kFont_16;
    titleLabel.textColor = UIColor.whiteColor;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.numberOfLines = 0;
    [scrollView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iconView.mas_bottom).offset(58);
        make.centerX.equalTo(scrollView);
        make.width.mas_equalTo(LYScreenWidth() - 30);
        make.left.equalTo(scrollView).offset(15);
        make.right.equalTo(scrollView).offset(-15);
    }];
    NSString *titleText = [NSString stringWithFormat:@"尊敬的%@，恭喜你成功报名参加\n“%@”",model.nickName,model.offlineName];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:titleText];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [titleText length])];
    titleLabel.attributedText = attributedString;

    UILabel *inviteLabel = [[UILabel alloc] init];
    inviteLabel.font = kFont_13;
    inviteLabel.textColor = UIColor.whiteColor;
    inviteLabel.text = @"邀请码";
    [scrollView addSubview:inviteLabel];
    [inviteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(45);
        make.centerX.equalTo(scrollView);
    }];


    NSArray *invateCodes = [model.inviteCode componentsSeparatedByString:@","];
    UIImageView *codeFrameImage = [[UIImageView alloc] init];
    [scrollView addSubview:codeFrameImage];
    UIImage *image = [UIImage imageWithContentsOfFile:SEGPicName(@"code_frame")];
    image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(7, 130, 7, 130) resizingMode:UIImageResizingModeStretch];
    codeFrameImage.image = image;
    CGFloat invateLabelHeight = 42.0;
    CGFloat invateLabelMargin = 12.0;
    [codeFrameImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(inviteLabel.mas_centerY);
        make.centerX.equalTo(scrollView);
        make.size.mas_equalTo(CGSizeMake(150, invateLabelHeight*invateCodes.count + invateLabelMargin*2));
    }];

    [invateCodes enumerateObjectsUsingBlock:^(NSString * _Nonnull code, NSUInteger idx, BOOL * _Nonnull stop) {
        UILabel *inviteCodeLabel = [[UILabel alloc] init];
        inviteCodeLabel.font = kFont_36;
        inviteCodeLabel.textColor = UIColor.whiteColor;
        inviteCodeLabel.text = code;
        [scrollView addSubview:inviteCodeLabel];
        [inviteCodeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(codeFrameImage.mas_top).offset(idx*invateLabelHeight + invateLabelMargin);
            make.centerX.equalTo(codeFrameImage);
            make.height.mas_equalTo(invateLabelHeight);
        }];
    }];



    UILabel *descLabel = [[UILabel alloc] init];
    descLabel.font = kFont_11;
    descLabel.textColor = UIColor.whiteColor;
    descLabel.numberOfLines = 0;
    descLabel.textAlignment = NSTextAlignmentCenter;
    NSString *descText = @"";

    if (model.preCount > 0) {
        descText = [NSString stringWithFormat:@"报名人数：%ld\n",(long)model.preCount];
    }
    if (model.giftName.length > 0) {
        descText = [descText stringByAppendingFormat: @"请在签到处出示此页面签到 \n成功签到后可以获得%@。",model.giftName];
    } else {
        descText = [descText stringByAppendingFormat:@"请在签到处出示此页面签到"];
    }
    NSMutableAttributedString *attributedString2 = [[NSMutableAttributedString alloc] initWithString:descText];
    [attributedString2 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [descText length])];
    descLabel.attributedText = attributedString2;

    [scrollView addSubview:descLabel];
    [descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(codeFrameImage.mas_bottom).offset(25);
        make.centerX.equalTo(scrollView);
    }];



    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.font = kFont_11;
    timeLabel.textColor = UIColor.whiteColor;
    NSString *time = [SEGUtils stringDateByFormatString:@"yyyy年MM月dd日 HH:mm" withDate:[NSDate dateWithTimeIntervalSince1970:[model.offlineTime floatValue]]];
    timeLabel.text = [NSString stringWithFormat:@"活动时间：%@",time];
    [scrollView addSubview:timeLabel];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(descLabel.mas_bottom).offset(48);
        make.centerX.equalTo(scrollView);
    }];

    UILabel *addressLabel = [[UILabel alloc] init];
    addressLabel.font = kFont_11;
    addressLabel.numberOfLines = 0;
    addressLabel.textColor = UIColor.whiteColor;
    addressLabel.textAlignment = NSTextAlignmentCenter;
    addressLabel.text = [NSString stringWithFormat:@"活动地点：%@",model.offlineAddr];
    [scrollView addSubview:addressLabel];
    [addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(timeLabel.mas_bottom).offset(8);
        make.left.mas_equalTo(15);
        make.centerX.equalTo(scrollView);
        make.bottom.mas_lessThanOrEqualTo(0);
    }];
    
}

#pragma mark - getter


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
